/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import wso.dto.WsoCustomer;
import wso.global.BaseDao;

/**
 *
 * @author Violet_Ink
 */
public class CustomerDaoImpl extends BaseDao implements CustomerDao {

    public void saveOrUpdateCustomer(WsoCustomer customer) {
        saveOrUpdate(customer);
    }

    public void deleteCustomer(String customerid) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("update WsoCustomer cust set cust.isdeleted = true where cust.customerid = :customerid");
        query = session.createQuery(str.toString());
        query.setString("customerid", customerid);

        query.executeUpdate();
    }

    public WsoCustomer[] getAllCustomer() {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();
        str.append("from WsoCustomer cust where cust.isdeleted = false");
        query = session.createQuery(str.toString());
        List userList = query.list();
        WsoCustomer[] result = new WsoCustomer[userList.size()];
        userList.toArray(result);

        return result;
    }

    public WsoCustomer getCustomer(String customerid) {
        Session session = getSession();
        Query query = null;
        StringBuilder str = new StringBuilder();

        str.append("from WsoCustomer cust where cust.customerid = :customerid and cust.isdeleted = false");
        query = session.createQuery(str.toString());
        query.setString("customerid", customerid);

        return (WsoCustomer) query.uniqueResult();
    }

}
